<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/11/23
 * Time: 10:23
 * Features: 学习配置
 */

namespace app\api\model;

use app\adminapiv3\service\module_manage\netfilter\FirewallSetService;
use app\api\model\learn_data\HostLearnDataModel;
use app\common\definition\HostDefinition;
use app\common\definition\netfilter\NetfilterConfigDefinition;
use app\common\exception\CustomException;
use app\common\model\HostLearnConfig;
use think\Db;

class HostLearnConfigModel extends BaseModel
{
    /**
     * 学习配置
     * @param string $hostKey 主机绑定码
    */
    public function study($hostKey){
        $host = Db::name('host')->where('hostkey',$hostKey)->find();
        if(!$host){
            throw new CustomException('主机不存在',422);
        }
        //读取学习配置数据
        $config = $this->where('host_id', $host['id'])->field('etime,have_mac_addr,model,danger_port,learn_number')->findOrEmpty()->toArray();

        if(!$config){
            throw new CustomException('主机学习配置不存在',422);
        }
        //剩余时间
        $remainTime = $config['etime'] - time();

        $remainTime = $remainTime <= 0 ? 0 : $remainTime;

        //学习模式 1累计学习模式 2全新学习模式
        if ($config['model'] == 2)
        {
            //操作子库中的学习数据
            $learnModel = new HostLearnDataModel();
            $learnModel->setHostId($host['id']);
            $learnModel->where('host_id', $host['id'])->delete();
        }

        //修改记录的回调状态
        $this->setCallBackStatus();

        //返回数据
        return [
            'Time' => $remainTime,
            'Mark' => $config['have_mac_addr'] ? true : false,
            'DangerPort' => $config['danger_port'] ? $config['danger_port'] : null,
            'LearnNumber' => $config['learn_number']
        ];
    }

    /**
     * 防火墙配置
     * @param string $hostKey 主机绑定码
    */
    public function firewallConfig($hostKey){
        $host = Db::name('host')->where('hostkey',$hostKey)->find();

        if(!$host){
            throw new CustomException('主机不存在', 422);
        }

        if ($host['os'] != HostDefinition::LINUX_OPERATION_SYSTEM)
        {
            throw new CustomException('该系统禁止访问该系统');
        }

        $firewall_set = Db::name('netfilter_firewall_config_linux')->field('main_switch,input,output,forward')->where('host_id', $host['id'])->find();
        $FirewallSetService = new FirewallSetService();
        $rule = $FirewallSetService->getLinuxFirewallSetRule($firewall_set);

        $firewallStatus = array_map(function($v){
            return $v ? true : false;
        }, $firewall_set);

        return [
            'Enable' => $firewallStatus['main_switch'],
            'In' => $firewallStatus['input'],
            'Out' => $firewallStatus['output'],
            'Forward' => $firewallStatus['forward'],
            'Rule' => $rule,
        ];
    }

    /**
     * 防火墙日志状态
     * @param string $hostKey 主机绑定码
     */
    public function firewallLogStatus($hostKey){
        $host = Db::name('host')->where('hostkey',$hostKey)->find();
        if(!$host){
            throw new CustomException('主机不存在',422);
        }
        $logStatus = $this->field('log_status')->where('host_id', $host['id'])->value('log_status');
        return [
            'Enable' => $logStatus ? true : false
        ];
    }

    /**
     * 将防火墙接管状态重置为已接管
     * @param array $param 表单数据
     * [
     *      'host_id' => 主机id
     * ]
    */
    public function setControlledFirewall($param){
        //查询学习配置
        $config = $this->where('host_id',$param['host_id'])->findOrEmpty()->toArray();
        if(!$config){
            throw new CustomException('主机学习配置不存在',204);
        }
        //修改防火墙接管状态为1
        $this->where('host_id',$param['host_id'])->setField('controlled_firewall',1);
        return true;
    }
}